package com.ptit.piclient;

import java.util.ArrayList;
import com.ptit.piclient.model.Album;
import com.ptit.piclient.tools.AllAlbumAdapter;
import com.ptit.piclient.tools.PiConnector;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

public class AllAlbumActivity extends Activity implements OnItemClickListener, TextWatcher{
	ListView lv;
	EditText txtSearchAlbum;
	ArrayList<Album> list = new ArrayList<Album>();
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_album_list);
		lv = (ListView) findViewById(R.id.allAlbumListView);
		lv.setOnItemClickListener(this);
		txtSearchAlbum = (EditText) findViewById(R.id.txtSearchAlbum);
		txtSearchAlbum.addTextChangedListener(this);
		new UpdateView(this).execute("");
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		//Oct 11
		Intent intent = new Intent(this,SA_Album_Details.class);
		intent.putExtra("albumid", list.get(position).getAlbumid());
		intent.putExtra("albumname",list.get(position).getAlbumtitle());
		startActivity(intent);
		//Oct 11
		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public void afterTextChanged(Editable arg0) {
		
	}
	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		
	}
	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		String key = txtSearchAlbum.getText().toString();
		new UpdateView(this).execute(key);
	}
	/*
	 * New thread to update listview
	 */
	private class UpdateView extends AsyncTask<String, Void, AllAlbumAdapter> {
		Activity act;
		ProgressDialog dialog;
		public UpdateView(Activity a) {
			act = a;
			dialog = new ProgressDialog(AllAlbumActivity.this);
			dialog.setTitle("");
			dialog.setMessage("Please wait...");
			dialog.setIndeterminate(true);
			dialog.show();
		}
		@Override
		protected void onProgressUpdate(Void... params){
			// Oct 11
			if(params[0].equals("") == false ) dialog.dismiss();
			// Oct11 end
			ProgressDialog dialog = new ProgressDialog(AllAlbumActivity.this);//, "", "Please wait...",true);
			dialog.setTitle("");
			dialog.setMessage("Please wait...");
			dialog.setIndeterminate(true);
			dialog.show();
			
		}
		@Override
		protected AllAlbumAdapter doInBackground(String... params) {
			list = PiConnector.getInstance(act).getAlbumsByKeyword(params[0]);
			AllAlbumAdapter ad = new AllAlbumAdapter(act, list);
			return ad;
		}
		
		@Override
		protected void onPreExecute() {

		}

		protected void onPostExecute(AllAlbumAdapter result) {
			dialog.dismiss();
			if (result != null)
				lv.setAdapter(result);
		}
	}
}
